﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealState.BusinessObjects;
using RealState.BLL;
using log4net;

namespace RealState.W
{
    public partial class Profile : System.Web.UI.Page
    {
        private Users userBO;
        private UsersBLL userBLL;
        private UsersAddressBLL userAddressBLL;
        private UsersAddress userAddressBO;
        private CitiesBLL citiesBLL;
        private DistrictsBLL districtBLL;
        protected ILog log = LogManager.GetLogger("Profile");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetCities();
                LoadData();
            }
        }
        public void LoadData()
        {
            int userID = Convert.ToInt32(Session["CustomerID"]);
            userBLL = new UsersBLL();
            userBO = new Users();
            userAddressBLL = new UsersAddressBLL();
            try
            {
                userBO = userBLL.GetUsers(userID);
                txtUserName.Text = userBO.UserName;
                txtFullname.Text = userBO.FullName;
                txtMobile.Text = userBO.Mobile;
                txtTel.Text = userBO.Tel;
                if (userBO.UserType == 1)
                    rdoUserType_1.Checked = true;
                else
                    rdoUserType_2.Checked = true;
                userAddressBO = userAddressBLL.GetUsersAddress(userID);
                drdCity.SelectedValue = Convert.ToString(userAddressBO.CityId);
                GetDistrict(Convert.ToInt32(userAddressBO.CityId));
                drdDistrict.SelectedValue = Convert.ToString(userAddressBO.DistrictId);
                txtStreet.Text = userAddressBO.Street;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        private void GetCities()
        {
            citiesBLL = new CitiesBLL();
            try
            {
                drdCity.DataSource = citiesBLL.GetCitiess();
                drdCity.DataTextField = "CityName";
                drdCity.DataValueField = "ID";
                drdCity.DataBind();
                drdCity.Items.Insert(0, new ListItem("-- Lựa chọn --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        private void GetDistrict(int cityId)
        {
            districtBLL = new DistrictsBLL();
            try
            {
                drdDistrict.DataSource = districtBLL.GetDistrictss().Where(d => d.CityID == cityId).ToList();
                drdDistrict.DataTextField = "DistrictName";
                drdDistrict.DataValueField = "ID";
                drdDistrict.DataBind();
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        protected void drdCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDistrict(Convert.ToInt32(drdCity.SelectedValue));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;
            userBO = new Users();
            userBLL = new UsersBLL();
            userAddressBO = new UsersAddress();
            userAddressBLL = new UsersAddressBLL();
            try
            {
                userBO.FullName = txtFullname.Text;
                userBO.Address = txtStreet.Text + " - " + drdDistrict.SelectedItem.Text + " - " + drdCity.SelectedItem.Text;
                userBO.Tel = txtTel.Text;
                userBO.Mobile = txtMobile.Text;
                userBO.UserType = (rdoUserType_1.Checked) ? 1 : 2;
                userBO.Id = Convert.ToInt32(Session["CustomerID"]);
                int result = userBLL.UpdateUsers(userBO);
                if (result > 0)
                {
                    userAddressBO.UserId = userBO.Id;
                    userAddressBO.CityId = Convert.ToInt32(drdCity.SelectedValue);
                    userAddressBO.DistrictId = Convert.ToInt32(drdDistrict.SelectedValue);
                    userAddressBO.Street = txtStreet.Text;
                    userAddressBLL.UpdateUsersAddress(userAddressBO);
                }
                ShowMsg("Thay đổi thông tin thành công.");
                Response.Redirect("Default.aspx");
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        public static void ShowMsg(string message)
        {
            // Cleans the message to allow single quotation marks 
            string cleanMessage = message.Replace("'", "\'");
            string script = "<script type='text/javascript'>alert('" + cleanMessage + "');</script>";
            // Gets the executing web page 
            Page page = HttpContext.Current.CurrentHandler as Page;
            // Checks if the handler is a Page and that the script isn't allready on the Page 
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(Profile), "alert", script);
            }
        }
    }
}
